<?php 
/**
	* Reg 
	*
	* @package Package Name
	* @subpackage Subpackage
	* @category Category
	* @author YinChuan  [hot530@126.com]
	* @link http://www.530Net.com
*/
class Reg extends MY_Controller {

	
	function __construct()
	{
		parent::__construct();
		$this->load->helper('box');
		$this->load->helper('form');
		$this->load->helper(array('exe','cookie'));
	}
	
	// 生成随机数
	public function randomkeys($length=9)
	{
		$pattern = 'abcdefghijklmnopqrstuvwxyz';    //字符池
		$key = '';
		for($i=0;$i<$length;$i++)
		{
			$key .= $pattern{mt_rand(0,25)};    //生成php随机数
		}
		return 'emailstr'.$key.date("YmdHis").rand(1000000,9999999);
	}

	// 会员注册第一步
	public function index()
	{
		$data = $this->data_config;
		$data['exeid']=form_hidden('exeid',getid_exe());
		$this->load->view('reg/reg1_view',$data);
	}
	
	/*	
		function:注册第二步：通过邮件确认
		note:防止访客直接访问这一步	 用cookie吧
	*/	
	public function reg2()
	{
		if(! isset($_POST["chkcode"]))  exit('非法访问');
		
		$exeid = $_POST["exeid"];
		// chk 验证码 必须加载的 session authcode
		$this->load->library('session');
		$this->load->library('authcode');
		$this->load->model('user_model');
		if(! $this->authcode->check($this->input->post('chkcode'))):
			header("content-type:text/html;charset=utf-8");
			echo "<script>alert(\"验证码错误！\");history.back();</script>";
		elseif(exist_exe($exeid) === 1):
			header("content-type:text/html;charset=utf-8");
			echo "<script>alert(\" 请勿重复刷新！\");</script>";
				$data = $this->data_config;
				$this->load->view('reg/reg2_view',$data);		
		else:
			// 生成 email验证字符
			$emailstr = $this->randomkeys();
			
			// 获得数据
			$formdata = array(
				'uname' 	=> $_POST["uname"],
				'gender' 	=> (int)$_POST["gender"],
				'mobile'	=> $_POST["mobile"],
				'email' 	=> $_POST["email"],
				'idcard' 	=> $_POST["idcard"],
				'upwd' 		=> $_POST["upwd"],
				'addressid' => $_POST["addressid"],
				'emailstr' 	=> $emailstr,
				'regok' =>0,
				'regtime' =>time(),
				'utype' =>1,   								// 免费商家
				'shopid' =>1
			);
			// 入库操作
			if($this->user_model->addreg($formdata) == 1)
			{
				letid_exe($exeid); // 入库操作时  写入cookie
				$data = $this->data_config;
				$activeurl = $data['base_header'].'/reg/reg3/'.$emailstr;
				$this->tomail($_POST["uname"],$activeurl,$_POST["email"]);// 发送email验证字符串
				$this->load->view('reg/reg2_view',$data);
			}
		endif;
	}
	
	/*
		note:防止访客直接访问这一步	 	用cookie吧
		function:注册第三步：注册成功  显示内容
		return:
	*/	
	public function reg3($emailstr="")
	{
		if($emailstr=="")
		{
			header("content-type:text/html;charset=utf-8");
			exit('非法访问！');
		}
		$this->load->model('user_model');
		if ($this->user_model->chkemailstr($emailstr)):
			$this->load->helper('num');
			
			$uaccount1 = $this->getuaccount();
			$uaccount2 = $this->getuaccount();
			$uaccount3 = $this->getuaccount();
			// 如果 $uaccount2 和 $uaccount1 相等的话，就重新拿号，直到不等
			while($uaccount2 === $uaccount1)
			{
				$uaccount2 = $this->getuaccount();
			}
			
			// 如果$uaccount3 等于$uaccount2 或$uaccount1 就要重新拿号
			while($uaccount3 === $uaccount2 OR $uaccount3 == $uaccount1)
			{
				$uaccount3 = $this->getuaccount();
			}
			
			$data = $this->data_config;
			$data['uaccount1']= $uaccount1;
			$data['uaccount2']= $uaccount2;
			$data['uaccount3']= $uaccount3;
			$attributes = array('onsubmit' => 'return expic()');

			$data['regformopen'] = form_open('reg/okuaccount', $attributes);
			$data['regformclose'] =form_close();
			$data['emailstr'] = form_hidden('emailstr',$emailstr);
			$userdata = $this->user_model->readuser(1,$emailstr)->row();
			$data['uname']=$userdata->uname;
			
			$this->load->view('reg/reg3_view',$data);
		else:
			header("content-type:text/html;charset=utf-8");
			exit('对不起，您的验证码不正确或者已经激活！');
		endif;
	}
	
	
// ------------------------------------------------------------------------
/*
	确认帐号
*/


	
	public function okuaccount()
	{
		
		if(! isset($_POST['emailstr']))
		{
			header("content-type:text/html;charset=utf-8");
			exit('非法访问！');
			
		} 
		$emailstr = $_POST['emailstr'];
		$uaccount = $_POST['uaccount'];
		$this->load->model('user_model');
		if($this->user_model->okuaccount($emailstr,$uaccount))
		{
			// 提示操作成功并将所有注册信息发送到邮箱里，提示若忘记密码可以登陆邮箱找回密码进入登陆页面
			$userdata = $this->user_model->readuser(0,$uaccount)->row();
$vbody =<<< EOT
			<h3>尊敬的家具369用户,您好！</h3>	<BR />
			以下是您的注册信息，请妥善保存：	<BR />
			姓 名： $userdata->uname		<BR />
			手 机： $userdata->mobile	<BR />
			邮 箱： $userdata->email	<BR />
			身份证： $userdata->idcard	<BR />
			<hr />
			帐 号： $userdata->uaccount	<BR />
			密 码： $userdata->upwd	<BR />
EOT;
			$vtitle = $uname = iconv("UTF-8","gb2312",'家具369会员注册信息');;
			$vbody = $uname = iconv("UTF-8","gb2312",$vbody);;
			$this->load->library('semail');
			$this->semail->kmail($userdata->email,$vtitle,$vbody);
			// 提示操作成功并将所有注册信息发送到邮箱里，提示若忘记密码可以登陆邮箱找回密码进入登陆页面
			$data = $this->data_config;
			$data['noticetitle'] = '会员注册成功';
			$noticebody =<<< EOT
					<h3>尊敬的家具369用户,您好！</h3><hr />	<BR />
					以下是您的注册信息，请妥善保存：	<BR />
			姓 名： $userdata->uname		<BR />
			手 机： $userdata->mobile	<BR />
			邮 箱： $userdata->email	<BR />
			身份证： $userdata->idcard	<BR />
			帐 号： $userdata->uaccount	<BR />
			密 码： $userdata->upwd	<BR />
					(注册信息已发送到您的注册邮箱！)&nbsp;&nbsp;&nbsp;<a href=" {$data['base_url'] }"><span class="redb">返回首页登陆 &gt;&gt;</span></a>
EOT;

	$data['noticebody'] = $noticebody;
	
	$this->load->view('notice_view',$data);	
			
		}else{
			header("content-type:text/html;charset=utf-8");
			exit('非法访问！');
		}
	}


	
// ------------------------------------------------------------------------

/**
	* 第一步：生号器（用户帐号）
	* 第一位不能是：0
	* Creates an anchor based on the local URL.
	*
	* @access	private
	* @param	
	* @param	
	* @param	
	* @return	string
*/
	private function getnum($length =6)
	{
		
		$key = '';
		$pattern = '1234567890';    //字符池
		for($i=0; $i<$length; $i++)
		{
			$key .= $pattern{mt_rand(0,9)};    //生成php随机数
		}
		return $key;
	}
// ------------------------------------------------------------------------

/**
	* 第二步：把号码过滤，如果在数据库存在就重新拿号
	*
	* Creates an anchor based on the local URL.
	*
	* @access	private
	* @param	string	the URL
	* @param	string	the link title
	* @param	mixed	any attributes
	* @return	string
*/
	private function getuaccount()
	{
		// 判断数据库中不存在
		$uaccount = '3'.$this->getnum(2).'6'.$this->getnum(2).'9'.$this->getnum(2);
		$this->load->model('user_model');
		while($this->user_model->chkuaccount($uaccount))
		{
		$uaccount = '3'.$this->getnum(2).'6'.$this->getnum(2).'9'.$this->getnum(2);
		}
	
		return $uaccount;
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
}
/* End of file reg.php */
/* Location: ./fitsys/application/me/controllers/reg.php */